package com.whfc.ms.api.fse.service.impl;

import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.fse.dto.FseCrawlerDTO;
import com.whfc.fse.dto.FseCrawlerDataDTO;
import com.whfc.fse.dto.FseCrawlerDataLogDTO;
import com.whfc.fse.dto.FseFvsDeviceDTO;
import com.whfc.fse.param.FseCrawlerAddParam;
import com.whfc.fse.param.FseCrawlerBindParam;
import com.whfc.fse.param.FseCrawlerEditParam;
import com.whfc.fse.param.FseFvsParam;
import com.whfc.fse.service.FseCrawlerService;
import com.whfc.fvs.dto.FvsDeviceDTO;
import com.whfc.fvs.service.FvsDeviceService;
import com.whfc.ms.api.fse.service.MsFseCrawlerService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author qzexing
 * @version 1.0
 * @date 2021/10/30 17:01
 */
@Service
public class MsFseCrawlerServiceImpl implements MsFseCrawlerService {


    @DubboReference(interfaceClass = FseCrawlerService.class, version = "1.0.0")
    private FseCrawlerService fseCrawlerService;

    @DubboReference(interfaceClass = FvsDeviceService.class, version = "1.0.0")
    private FvsDeviceService fvsDeviceService;


    @Override
    public PageData<FseCrawlerDTO> list(Integer pageNum, Integer pageSize, Integer deptId, String keyword, Integer bindFlag, Integer netState) {
        return fseCrawlerService.list(pageNum, pageSize, deptId, keyword, bindFlag, netState);
    }

    @Override
    public ListData<FseCrawlerDTO> list(Integer deptId, Integer bindFlag, Integer netState) {
        return fseCrawlerService.list(deptId, bindFlag, netState);
    }

    @Override
    public FseCrawlerDTO detail(Integer crawlerId) {
        return fseCrawlerService.detail(crawlerId);
    }

    @Override
    public void add(FseCrawlerAddParam request) {
        fseCrawlerService.add(request);
    }

    @Override
    public void edit(FseCrawlerEditParam request) {
        fseCrawlerService.edit(request);
    }

    @Override
    public void del(Integer crawlerId) {
        fseCrawlerService.del(crawlerId);
    }

    @Override
    public void bind(FseCrawlerBindParam request) {
        fseCrawlerService.updateBindState(request);
    }

    @Override
    public void unbind(FseCrawlerBindParam request) {
        fseCrawlerService.updateBindState(request);
    }

    @Override
    public FseCrawlerDataDTO getLatest(Integer crawlerId) {
        return fseCrawlerService.getLatest(crawlerId);
    }

    @Override
    public List<FseCrawlerDataLogDTO> getCrawlerLog(Integer crawlerId, Date date, String field) {
        return fseCrawlerService.getCrawlerLog(crawlerId, date, field);
    }

    @Override
    public void bindFvs(FseFvsParam request) {
        fseCrawlerService.bindFvs(request);
    }

    @Override
    public List<FseFvsDeviceDTO> fvsList(Integer crawlerId) {
        List<FseFvsDeviceDTO> list = fseCrawlerService.getFvsDeviceList(crawlerId);
        if (list == null || list.isEmpty()) {
            return new ArrayList<>();
        }
        //取出设备ID 查找监控设备信息
        Map<Integer, FseFvsDeviceDTO> map = new HashMap<>();
        for (FseFvsDeviceDTO fvsDeviceDTO : list) {
            map.put(fvsDeviceDTO.getFvsDeviceId(), fvsDeviceDTO);
        }
        List<FvsDeviceDTO> fvsDeviceList = fvsDeviceService.list(new ArrayList<>(map.keySet()));
        for (FvsDeviceDTO fvsDeviceDTO : fvsDeviceList) {
            FseFvsDeviceDTO fseFvsDeviceDTO = map.get(fvsDeviceDTO.getId());
            if (fseFvsDeviceDTO != null) {
                fseFvsDeviceDTO.setFvsDevice(fvsDeviceDTO);
            }
        }
        return new ArrayList<>(map.values());
    }
}
